package com.atguigu.serviceedu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.serviceedu.entity.EduSubject;
import com.atguigu.serviceedu.entity.ExcelSubject.ExcelSubjectData;
import com.atguigu.serviceedu.mapper.EduSubjectMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.security.auth.Subject;
import java.util.UUID;

public class ExcelSubjectDataListener extends AnalysisEventListener<ExcelSubjectData> {

    private EduSubjectMapper eduSubjectMapper;
    public ExcelSubjectDataListener(){

    }
    public ExcelSubjectDataListener(EduSubjectMapper eduSubjectMapper){
        this.eduSubjectMapper=eduSubjectMapper;
    }
    @Override
    public void invoke(ExcelSubjectData data, AnalysisContext analysisContext) {
        String levelOneTitle = data.getLevelOneTitle();
        String levelTwoTitle = data.getLevelTwoTitle();
        //走到这里我们需要将excel里面的一行记录进行拆分,目的是拆分出一级分类，然后重新装入和表关联的
        //subject对象里面，插入表中。想法是好的，但是根据excel表的结构，你会发现一级分类名是有重复的
        //所一首先要判断表中是否已经有了同名的且parentid为0一级分类
        EduSubject levelOneSubject =this.checklevelOneTitleIsExist(levelOneTitle);
        String levelTwoSubjectParentId=null;//这里定义一个parentid是来接二级分类的parentid的，当然它就是一级分类的id
        if (levelOneSubject == null){
            //走到这里证明数据库里面没有这么一个一级分类，我下面的操作就是第一次插,但是得首先构造一个edusubject对象
            EduSubject insetLevelOneSubject = new EduSubject();
            insetLevelOneSubject.setTitle(levelOneTitle);
            insetLevelOneSubject.setParentId("0");
            //insetLevelOneSubject.setId(UUID.randomUUID().toString());
            eduSubjectMapper.insert(insetLevelOneSubject);
            levelTwoSubjectParentId=insetLevelOneSubject.getId();
        }else {
            levelTwoSubjectParentId=levelOneSubject.getId();
        }
        EduSubject levelTwoSubject = this.checklevelTwoTitleInTheSameLevelOneTitleIsExist(levelTwoTitle, levelTwoSubjectParentId);
        if (levelTwoSubject==null){
            EduSubject insetLevelTwoSubject = new EduSubject();
            insetLevelTwoSubject.setTitle(levelTwoTitle);
            insetLevelTwoSubject.setParentId(levelTwoSubjectParentId);
            eduSubjectMapper.insert(insetLevelTwoSubject);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
    private EduSubject checklevelOneTitleIsExist(String title){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",title);
        queryWrapper.eq("parent_id","0");
        EduSubject eduSubject = eduSubjectMapper.selectOne(queryWrapper);
        return eduSubject;
    }
    private EduSubject checklevelTwoTitleInTheSameLevelOneTitleIsExist(String levelTwoTitle,String parentId){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",levelTwoTitle);
        queryWrapper.eq("parent_id",parentId);
        EduSubject subject = eduSubjectMapper.selectOne(queryWrapper);
        return subject;
    }
}
